package merisis.contentcloud.impl;

import static java.lang.String.format;

import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import merisis.MerisisVM;
import merisis.contentcloud.ContentCloud;

import com.google.inject.Inject;

public class ContentCloudImpl implements ContentCloud {

	@Inject
	private Logger logger;

	@Inject
	private MerisisVM cvm;

	private HashMap<Long, Object> base = new HashMap<Long, Object>();

	public long occur(Object content) {

		long cid = cvm.occur();

		synchronized (base) {
			base.put(cid, content);
		}
		
		if (logger.isLoggable(Level.FINE)) {
			logger.fine(format("occurrence, cid: '%d', '%s'", cid,
					(content != null) ? content.getClass().getName() : "null"));
		}

		return cid;
	}

	@SuppressWarnings("unchecked")
	public <T> T retrieve(long cid) {

		Object cnt;
		synchronized (base) {
			cnt = base.get(cid);
		}

		if (logger.isLoggable(Level.FINE)) {
			logger.fine(format("retrived, cid: '%d', '%s'", cid,
					(cnt != null) ? cnt.getClass().getName() : "null"));
		}

		return (T) cnt;
	}
}